//
//  MJRefreshGifHeader.swift
//  OC-Swift
//
//  Created by 陆文宇 on 2017/2/14.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import UIKit

class MJRefreshGifHeader: MJRefreshStateHeader {
    
    lazy var gifView: UIImageView = UIImageView()
    lazy var stateImages: Dictionary<MJRefreshState, Array<UIImage>> = [:]
    lazy var stateDurations: Dictionary<MJRefreshState, TimeInterval> = [:]
    override var pullingPercent: CGFloat? {
        set {
            super.pullingPercent = newValue
            
            if let images: Array<UIImage> = stateImages[.idle] {
                if state != .idle || images.count == 0 {
                    return
                }
                gifView.stopAnimating()
                var index: Int = images.count * Int(newValue!)
                if index >= images.count {
                    index = images.count - 1
                }
                gifView.image = images[index]
            }
        }
        
        get {
            return _pullingPercent
        }
    }
    override var state: MJRefreshComponent.MJRefreshState {
        set {
            if super.state != newValue {
                super.state = newValue
            }
            
            if state == .pulling || state == .refreshing {
                if let images: Array<UIImage> = stateImages[state] {
                    if images.count == 0 {
                        return
                    }
                    
                    gifView.stopAnimating()
                    if images.count == 1 {
                        gifView.image = images.last!
                    } else {
                        gifView.animationImages = images
                        gifView.animationDuration = stateDurations[state]!
                        gifView.startAnimating()
                    }
                }
            } else if state == .idle {
                gifView.stopAnimating()
            }
        }
        
        get {
            return _state
        }
    }
    override init(refreshingClosure: @escaping MJRefreshComponentRefreshingClosure) {
        super.init(refreshingClosure: refreshingClosure)
        
        addSubview(gifView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
        
    func setImages(_ images: Array<UIImage>, duration: TimeInterval, forState state: MJRefreshState) -> Void {
        stateImages[state] = images
        stateDurations[state] = duration
        
        let image: UIImage = images.first!
        if image.size.height > mj_h {
            mj_h = image.size.height
        }
    }
        
    func setImages(_ images: Array<UIImage>, forState state: MJRefreshState) -> Void {
        setImages(images, duration: TimeInterval(images.count) * 0.1, forState: state)
    }
    
    override func prepare() {
        super.prepare()
        
        labelLeftInset = 20
    }
    
    override func placeSubviews() {
        super.placeSubviews()
        
        if gifView.constraints.count > 0 {
            return
        }
        
        gifView.frame = bounds;
        if stateLabel.isHidden && lastUpdatedTimeLabel.isHidden {
            gifView.contentMode = .center
        } else {
            gifView.contentMode = .right
            
            let stateWidth: CGFloat = stateLabel.mj_textWith
            var timeWidth: CGFloat = 0
            if !lastUpdatedTimeLabel.isHidden {
                timeWidth = lastUpdatedTimeLabel.mj_textWith
            }
            let textWidth: CGFloat = max(stateWidth, timeWidth)
            gifView.mj_w = mj_w * 0.5 - textWidth * 0.5 - labelLeftInset!
        }
    }

}
